package com.weixin.project.Service;

import com.weixin.project.Dao.QuestionEmbDao;
import com.weixin.project.Dao.UserEmbDao;
import com.weixin.project.Entity.QuestionEmb;
import com.weixin.project.Entity.User;
import com.weixin.project.Entity.UserEmb;
import org.hibernate.annotations.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class EmbService {
    @Autowired
    UserEmbDao userEmbDao;
    @Autowired
    QuestionEmbDao questionEmbDao;
    @Autowired
    RedisTemplate redisTemplate;
    private final static String USER_EMB = "user_emb";
    private final static String QUESTION_EMB = "question_emb";
    @Cacheable(value = QUESTION_EMB, key = "#id", unless = "#result == null ")
    public QuestionEmb findQuestionEmb(Integer id){
        Optional<QuestionEmb> embOptional = questionEmbDao.findById(id);
        return embOptional.isPresent()?embOptional.get():null;
    }

    @Cacheable(value = USER_EMB, key = "#id", unless = "#result == null ")
    public UserEmb findUserEmb(Integer id){
        Optional<UserEmb> embOptional = userEmbDao.findById(id);
        return embOptional.isPresent()?embOptional.get():null;
    }
}
